/*****************************************************************************
 *  This file is part of the OpenDomo project.
 *  Copyright(C) 2011 OpenDomo Services SL
 *  
 *  Daniel Lerch Hostalot <dlerch@opendomo.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/


// ----------------------------------------------------------------------------
// author: Daniel Lerch <dlerch@gmail.com>, http://opendomo.com
// date: Mayo 2007
//
// CHANGES:
//
// ----------------------------------------------------------------------------

#include <stdlib.h>
#include <upnp_query.h>

// {{{ upnp_query_print_usage()
void upnp_query_print_usage(const char *progname)
{
   printf(
   "\n"
   "Usage: %s listen  <time>\n"
   "       %s info    <XML device desc> \n"
   "       %s xml     [<field name>|doc] <XML url> \n"
   "       %s get     <varname>  <action url>\n"
   "       %s action  <name> <csv param=value> <action url> <service type>\n"
   "\n\n", 
   progname, progname, progname, progname, progname);
}

// }}}

int main(int argc, char **argv)
{
   if(argc < 3)
   {
      upnp_query_print_usage(argv[0]);
      return 0;
   }

   // {{{ Listen UPnP
   if(strcmp(argv[1], "listen")==0)
   {
      if(upnp_query_print_device_list(atoi(argv[2])) != 0)
      {
         printf("list error\n");
         exit(EXIT_FAILURE);
      }
   }
   // }}}

   // {{{ Device XML info 
   else if(strcmp(argv[1], "info")==0)
   {
      const char *xml_dev_desc = argv[2];

      if(!xml_dev_desc)
      {
         upnp_query_print_usage(argv[0]);
         return 0;
      }

		if(upnp_query_print_xmlinfo(xml_dev_desc) != 0)
      {
         printf("query info error\n");
         return 0;
      }

   }
   // }}}

   // {{{ Query XML 
   else if(strcmp(argv[1], "xml")==0)
   {
      const char *param = argv[2];
      const char *xml_url = argv[3];

      if(!xml_url || !param)
      {
         upnp_query_print_usage(argv[0]);
         return 0;
      }

      if(strcmp(param, "doc")==0)
      {
         if(upnp_query_print_xmldoc(xml_url) != 0)
         {
            printf("queryxml error\n");
            return 0;
         }
         return 0;
      }

      char value[256];

      if(upnp_query_query_xml_field(param, xml_url, value, sizeof(value)) != 0)
      {
         printf("queryxml error\n");
         return 0;
      }

      printf("%s\n", value);
   }
   // }}}

   // {{{ Get variable value 
   else if(strcmp(argv[1], "get") == 0)
   {
      const char* varname = argv[2];
      const char* action_url = argv[3];

      if(!varname || !action_url)
      {
         upnp_query_print_usage(argv[0]);
         return 0;
      }

      if(upnp_query_get_var(varname, action_url) != 0)
      {
         printf("get error\n");
         exit(EXIT_FAILURE);
      }
   }
   // }}}

   // {{{ Execute action
   else if(strcmp(argv[1], "action") == 0)
   {
      const char *action_name = argv[2];
      const char *csv_params = argv[3];
      const char *action_url = argv[4];
      const char *service_type = argv[5];

      if(!action_name || !csv_params || !action_url || !service_type)
      {
         upnp_query_print_usage(argv[0]);
         return 0;
      }

      if(upnp_query_exec_action(action_name,csv_params,action_url,service_type)!=0)
      {
         printf("action error\n");
         exit(EXIT_FAILURE);
      }
   }
   // }}}

   /* unknown */
   else
   {
      upnp_query_print_usage(argv[0]);
      return 0;
   }




   return 0;
}


